/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gp.vlc.rp.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author Namo
 */
public class JPanelButton extends javax.swing.JPanel {

    private int inactive = 255;
    private int hover = 240;
    private int active = 230;
    
    private int target = 255;
    
    private Timer fastTimer;
    private Timer slowTimer;

    /**
     * Creates new form JPanelButton
     */
    public JPanelButton() {
        initComponents();
        setFocusable(true);
        setBackground(new Color(inactive, inactive, inactive));

        ActionListener fastActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                int red = getBackground().getRed();
                if (red < target) {
                    red++;
                }
                else if (red > target) {
                    red--;
                }
                else {
                    fastTimer.stop();
                }
                setBackground(new Color(red, red, red));
            }
        };
        fastTimer = new Timer(5, fastActionListener);

        ActionListener slowActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                int red = getBackground().getRed();
                if (red < target) {
                    red++;
                }
                else if (red > target) {
                    red--;
                }
                else {
                    fastTimer.stop();
                }
                setBackground(new Color(red, red, red));
            }
        };
        slowTimer = new Timer(20, slowActionListener);;
    }

    public void setText(String text) {
        this.jLabelText.setText(text);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelText = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(160, 160, 160)));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                formFocusLost(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });

        jLabelText.setFont(jLabelText.getFont().deriveFont(jLabelText.getFont().getStyle() | java.awt.Font.BOLD));
        jLabelText.setForeground(new java.awt.Color(102, 102, 102));
        jLabelText.setText("JButtonPanel");
        jLabelText.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelText, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabelText)
                .addGap(4, 4, 4))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
        target = hover;
        slowTimer.stop();
        fastTimer.start();
    }//GEN-LAST:event_formMouseEntered

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        if (!isFocusOwner()) {
            target = inactive;
            fastTimer.stop();
            slowTimer.start();
        }
    }//GEN-LAST:event_formMouseExited

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        fastTimer.stop();
        slowTimer.stop();
        setBackground(new Color(active, active, active));
    }//GEN-LAST:event_formMousePressed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        formMouseEntered(null);
    }//GEN-LAST:event_formFocusGained

    private void formFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusLost
        formMouseExited(null);
    }//GEN-LAST:event_formFocusLost

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        fastTimer.stop();
        slowTimer.start();
    }//GEN-LAST:event_formMouseReleased

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        formMousePressed(null);
    }//GEN-LAST:event_formKeyPressed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
        formMouseReleased(null);
    }//GEN-LAST:event_formKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelText;
    // End of variables declaration//GEN-END:variables
}
